package se.webbzon.boltzmann.battle.strategy.playerstrategy;

import java.awt.Image;

import se.webbzon.boltzmann.battle.hud.AbstractBattleHudLayout;
import se.webbzon.boltzmann.input.Input;
import se.webbzon.boltzmann.input.Button;
import se.webbzon.boltzmann.input.InputListener;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class PlayerStrategyHud extends WorldObject {
	
	// The 2-dimensional depth of the strategy heads-up display
	private static final int depth = -500;
	
	// The player strategy associated to this player strategy heads-up display 
	private final PlayerStrategy strategy;
	
	// The current page of the player strategy heads-up display
	private AbstractPSPage page;
	
	// The input listener associated with this player strategy heads-up display
	private InputListener inputListener;
	
	/*============================================================
	Constructors
	============================================================*/

	public PlayerStrategyHud(	PlayerStrategy strategy,
								AbstractBattleHudLayout layout) {
		super(	WorldObject.onCreate + WorldObject.onStep + 
				WorldObject.onDestroy + WorldObject.onPostDraw, depth);
		this.strategy = strategy;
		
		// Create root icon page
		PSIconPage iconPage = new PSIconPage(layout.getIconPageLayout());
		
		AbstractPSPage strategy1, character, item, combo, special;
		strategy1 = new PSEmptyPage(iconPage);
		character = new PSCharacterIconPage(iconPage,layout.getIconPageLayout(),strategy.getPlayer().getCharacter());
		item = new PSItemPage(iconPage,layout.getItemPageLayout(),strategy.getPlayer());
		combo = new PSEmptyPage(iconPage);
		special = new PSEmptyPage(iconPage);
		
		Image st,ch,it,co,sp;
		st = Resources.getImageSet("icon_harden").get(0);
		ch = Resources.getImageSet("icon_character").get(0);
		it = Resources.getImageSet("icon_item").get(0);
		co = Resources.getImageSet("icon_combo").get(0);
		sp = Resources.getImageSet("icon_special").get(0);
		
		iconPage.addIcon(new PSAvailableLinkIcon(strategy1,st,"Strategy",""));
		iconPage.addIcon(new PSAvailableLinkIcon(character,ch,"Character",""));
		iconPage.addIcon(new PSAvailableLinkIcon(item,it,"Item",""));
		iconPage.addIcon(new PSAvailableLinkIcon(combo,co,"Combo",""));
		iconPage.addIcon(new PSAvailableLinkIcon(special,sp,"Special",""));
		
		page = iconPage;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the player strategy associated to this player strategy
	 * heads-up display. **/
	public PlayerStrategy getStrategy() {
		return strategy;
	}
	
	/** Sets the player strategy page for this player strategy heads-up
	 * display. **/
	public void setPage(AbstractPSPage page) {
		this.page = page;
	}
	
	/** Returns the current player strategy page for this player 
	 * strategy heads-up display. **/
	public AbstractPSPage getPage() {
		return page;
	}
	
	@Override public void onCreate(WorldArea world) {
		inputListener = new InputListenerImpl();
		strategy.getPlayer().getInput().addListener(inputListener);
	}
	
	@Override public void onStep(WorldArea world) {
		page.onStep(this);
	}
	
	@Override public void onDestroy(WorldArea world) {
		strategy.getPlayer().getInput().removeListener(inputListener);
		inputListener = null;
	}
	
	@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
		page.onDraw(this, hud);
	}

	/*============================================================
	Private Classes
	============================================================*/
	
	private final class InputListenerImpl implements InputListener {

		@Override public void primaryAxisChanged(	Input input, double direction,
													double value) {};

		@Override public void secondaryAxisChanged(	Input input, double direction,
													double value) {};

		@Override public void buttonPressed(Input input, Button button) {
			if (button == Button.ACTION)
				page.onSelect(PlayerStrategyHud.this);
			else if (button == Button.SPECIAL)
				page.onReturn(PlayerStrategyHud.this);
			else
				page.onPress(PlayerStrategyHud.this, button);
		}

		@Override public void buttonReleased(Input input, Button button) {};
		
		@Override public void bindingAccepted(Input input, Button button) {};
		
	}
	
}
